package com.zheng.pvz.model.proxy
{
	import com.zheng.pvz.model.vo.PlantVo;
	import com.zheng.pvz.model.vo.ShellVo;
	import com.zheng.pvz.model.vo.ZombiesVo;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	
	/**
	 * 游戏数据
	 * @author Administrator
	 * 
	 */
	public class GameProxy extends EventDispatcher
	{
		public var tileProxyArr:Array;//地砖
		public var plantProxyArr:Array;//植物
		public var zombiesProxyArr:Array;//僵尸
		public var shellProxyArr:Array;//子弹
		
		public var tempAddProxyArr:Array;//临时存储
		
		public static const ADD_ELEMENT:String="add_ELEMENT";
		public static const DEL_ELEMENT:String="del_ELEMENT";
		
		public function GameProxy()
		{
			this.tileProxyArr=[];
			this.plantProxyArr=[];
			this.zombiesProxyArr=[];
			this.shellProxyArr=[];
			this.tempAddProxyArr=[];
		}
		/**
		 * 添加proxy 
		 * @param proxy
		 * 
		 */
		public function addProxy(proxy:DisplayProxy):void{
			this.tempAddProxyArr.push(proxy);
			proxy.addEventListener(DisplayProxy.PROXY_DEL,onDelProxy);
			//僵尸的位置变化
			if(proxy is ZombiesProxy)proxy.addEventListener(DisplayProxy.CHANGED_AREA,onAreaChanged);
			this.dispatchEvent(new Event(GameProxy.ADD_ELEMENT));
		}
		/**
		 * 删除proxy 
		 * @param proxy
		 * 
		 */
		public function delProxy(proxy:DisplayProxy):void{
			var index:int;
			var type:String=proxy.vo.displayType;
			var arr:Array;
			switch(type){
				case PlantVo.TYPE:
					index=this.plantProxyArr.indexOf(proxy);
					arr=this.plantProxyArr;
					break;
				case ZombiesVo.TYPE:
					index=this.zombiesProxyArr.indexOf(proxy);
					arr=this.zombiesProxyArr;
					break;
				case ShellVo.TYPE:
					index=this.shellProxyArr.indexOf(proxy);
					arr=this.shellProxyArr;
					break;
				default:
					trace("code info GameProxy.as没有的情况。")
			}
			arr.splice(index,1)
		}
		private function onDelProxy(e:Event):void{
			var proxy:DisplayProxy=e.currentTarget as DisplayProxy;
			this.delProxy(proxy);
			if(proxy is ZombiesProxy){
				onZombiesProxy(proxy);
				proxy=null;
			};
		}
		/**
		 * 僵尸所在区域改变
		 * 查找关心僵尸位置的植物 
		 * 
		 * @param e
		 * 
		 */
		private function onAreaChanged(e:Event=null):void{
			var zombiesProxy:DisplayProxy=e.target as DisplayProxy;
			for each(var i:PlantProxy in this.plantProxyArr){
				var areaOfInterest:Array=PlantVo(i.vo).areaOfInterest;
				var zombiesOfInterest:Array=PlantVo(i.vo).zombiesOfInterest;
				var index:int=zombiesOfInterest.indexOf(zombiesProxy);
				if(index!=-1){//在列表中
					if(!isZombiesInArea(zombiesProxy,areaOfInterest)){//不在关心区域
						zombiesOfInterest.splice(index,1);
					}
				}else{//不在列表中
					if(isZombiesInArea(zombiesProxy,areaOfInterest)){
						zombiesOfInterest.push(zombiesProxy);
					}
				}
				i.isInvade=Boolean(zombiesOfInterest.length);
			}
		}
		/**
		 * 僵尸挂掉后 
		 * @param zombiesProxy
		 * 
		 */
		private function onZombiesProxy(zombiesProxy:DisplayProxy):void{
			for each(var i:PlantProxy in this.plantProxyArr){
				var zombiesOfInterest:Array=PlantVo(i.vo).zombiesOfInterest;
				var index:int=zombiesOfInterest.indexOf(zombiesProxy);
				if(index!=-1){
					zombiesOfInterest.splice(index,1);
				}
				i.isInvade=Boolean(zombiesOfInterest.length);
			}
		}
		/**
		 * 检测zombiesProxy是否在plant所关心的区域 
		 * @param zombiesProxy
		 * @param areaOfInterest
		 * @return 
		 * 
		 */
		private function isZombiesInArea(zombiesProxy:DisplayProxy,areaOfInterest:Array):Boolean{
			var interestNum:int=areaOfInterest.length;
			for(var i:int=0;i<interestNum;i++){
				if(zombiesProxy.row==Point(areaOfInterest[i]).x&&zombiesProxy.cols==Point(areaOfInterest[i]).y){
					return true;
				}
			}
			return false;
		}
		
	}
}